import pandas as pd
import numpy as np
def diagnostics(port_ret):#port_ret为日收益率序列（带百分比号），list或array
    port_returns = pd.DataFrame(port_ret)
    port_returns.columns=['Portfolio']
    mean_return = port_returns.mean()*250 #计算平均收益率，一年中近似算250个交易日
    volatility = port_returns.std()*250#计算波动率
    # 这个0.3是无风险利率，可以自己调整
    sp_ratio=(mean_return-0.03)/(volatility**0.5)#计算夏普比率
    performance=pd.DataFrame({'Annuaised Return': mean_return,'St. Dev.': volatility,'Sharpe Ratio':sp_ratio})
    port_values = port_returns.cumsum().apply(np.exp)
    rolling_peak = port_values.rolling(250).max()
    drawdown = np.log(port_values/rolling_peak)
    max_drawdown = drawdown.min().rename('Max Drawdown')#计算最大回撤率
    performance = performance.join(max_drawdown)
    return performance.transpose()
